<template>
  <el-row>
    <el-tree
      ref="tree"
      :data="data"
      show-checkbox
      node-key="id"
      label="menuName"
      :props="defaultProps"
      :default-checked-keys="defaultCheckedKeys"
    />
  </el-row>
</template>

<script>
import { GetTreeNodes } from '@/api/menu'
import { GetTreeIds, SetPermission } from '@/api/role'

export default {
  name: 'VueElementAdminPermission',

  props: {
    role: {
      type: Object,
      default() {
        return {}
      }
    }
  },

  data() {
    return {
      data: [],
      defaultProps: {
        children: 'child',
        label: 'menuName'
      },
      defaultCheckedKeys: []
    }
  },

  mounted() {
    GetTreeNodes().then(res => {
      this.data = res.data
    })
    GetTreeIds({ roleId: this.role.id }).then(res => {
      console.log(res)
      this.defaultCheckedKeys = res.data
    })
  },

  methods: {
    submitForm(formName) {
      const _this = this
      var menuIds = this.$refs.tree.getCheckedNodes(false, true).map(x => x.id)
      SetPermission({ roleId: this.role.id }, menuIds).then(res => {
        this.$message({
          message: res.message,
          type: 'success',
          onClose: () => {
            _this.$emit('success')
          }
        })
      })
    }
  }
}
</script>
